package com.liuheng.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.spzx.model.entity.product.Category;
import com.liuheng.spzx.model.entity.product.ProductSku;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.h5.IndexVo;
import com.liuheng.spzx.product.service.CategoryService;
import com.liuheng.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name="首页接口管理")
@RestController
@RequestMapping("/api/product/index")
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductSkuService productSkuService;
  @GetMapping("")
    public Result<IndexVo> getCategory(){
      LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(Category::getParentId,0);
      List<Category> categoryList = categoryService.list(wrapper);
      LambdaQueryWrapper<ProductSku> wrapper1 = new LambdaQueryWrapper<>();
      wrapper1.orderByDesc(ProductSku::getSaleNum);
      Page<ProductSku> productSkuPage = new Page<>(1,10);
      List<ProductSku> productSkuList = productSkuService.page(productSkuPage, wrapper1).getRecords();
      IndexVo indexVo = new IndexVo();
      indexVo.setCategoryList(categoryList);
      indexVo.setProductSkuList(productSkuList);
      return Result.build(indexVo, ResultCodeEnum.SUCCESS);
  }
}
